//
//  ViewController.m
//  musicplay
//
//  Created by cjz on 16/12/19.
//  Copyright © 2016年 apple-1511. All rights reserved.
//

#import "ViewController.h"
#import "music.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    [self setviewImage];
    [self initData];
}
-(void)initData{
    //基本属性数据
    _isplay=NO;//是否正在播放
    _iscircle=YES;//是否循环
    _lrcnumber=0;//歌词的行号
    //歌曲的加载
    [self initSongs];
    _musicNumber = 2;
        //初始化播放器
    NSString *songPath=[[NSBundle mainBundle]pathForResource:[_musicarray[_musicNumber]name] ofType:@"mp3"];
    NSURL *readSong=[NSURL fileURLWithPath:songPath];
    _player=[[AVAudioPlayer alloc]initWithContentsOfURL:readSong error:nil];
    [_player prepareToPlay];
    //设置当前播放歌曲
    _currentmusic=_musicarray[_musicNumber];
    //歌词的加载
    [self initLrc];
    //音量的初始化
    _player.volume=0.1;//初始化播放器的音量为0.1
    //播放进度到初始化
    _soundSlider.value=_player.volume;//设置界面音量滑块的位置
    
}
-(void)initSongs{
    //将三首歌封装到musicarray数组
    music *m1=[[music alloc]initWithName:@"背对背拥抱" andType:@"mp3"];
    music *m2=[[music alloc]initWithName:@"偶阵雨" andType:@"mp3"];
    music *m3=[[music alloc]initWithName:@"情非得已" andType:@"mp3"];
    _musicarray=[[NSMutableArray alloc]initWithCapacity:10];
    [_musicarray addObjectsFromArray:@[m1,m2,m3]];
    
}
-(void)initLrc{
    _timearray=[[NSMutableArray alloc]initWithCapacity:10];
    _lrcdictionary=[NSMutableDictionary dictionary];
    //根据当前播放的歌曲的对应歌词
    NSString *lrcpath=[[NSBundle mainBundle]pathForResource:[_musicarray[_musicNumber]name] ofType:@"txt"];
    NSString *contentStr=[NSString stringWithContentsOfFile:lrcpath encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"%@",contentStr);
    NSArray *array=[contentStr componentsSeparatedByString:@"\n"];
    //循环处理歌词保存到歌词字典
    for(int i=0;i<[array count];i++){
        NSString *lineStr=[array objectAtIndex:i];
        //将拿到的歌词拆分成歌词和时间两部分
        NSArray *linearray=[lineStr componentsSeparatedByString:@"]"];
               if([linearray[0]length]>8){
                   //是不是歌词时间第七个字符为.第4个字符为：）
        NSString *s1=[lineStr substringWithRange:NSMakeRange(3, 1)];
        NSString *s2=[lineStr substringWithRange:NSMakeRange(6, 1)];
     //NSLog(@"%@,%@",s1,s2);
    if([s1 isEqualToString:@":"]&&[s2 isEqualToString:@"."]){
        NSString *lrc=[linearray objectAtIndex:1];
        NSString *time=[linearray[0]substringWithRange:NSMakeRange(1, 5)];
        [_lrcdictionary setObject:lrc forKey:time];
        [_timearray addObject:time];
            }
        }
    }
}
-(void)initViews
{
    //屏幕宽
    CGFloat sWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat sHeight = [UIScreen mainScreen].bounds.size.height;
    self.backview=[[UIImageView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_backview];
    //音量
    _soundSlider = [[UISlider alloc]initWithFrame:CGRectMake(0, 44, sWidth, 40)];
    [self.view addSubview:_soundSlider];
    
    //歌词显示的TableView
    _lrcTableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 44+30, sWidth, sHeight-44-30-30-60) style:UITableViewStylePlain];
    _lrcTableView.delegate=self;
    _lrcTableView.dataSource=self;
    _lrcTableView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:_lrcTableView];
    //当前时间显示
    _currentTimeLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 44+30+self.lrcTableView.frame.size.height, 55, 30)];
    _currentTimeLable.text = @"00:00";
    [self.view addSubview:_currentTimeLable];
    //播放
    _progressView = [[UISlider alloc]initWithFrame:CGRectMake(50, 44+30+self.lrcTableView.frame.size.height, sWidth-110, 30)];
    [self.view addSubview:_progressView];
    //总时间显示
    _totalTimeLable = [[UILabel alloc]initWithFrame:CGRectMake(sWidth-55, 44+30+self.lrcTableView.frame.size.height, 55, 30)];
    _totalTimeLable.text = @"00:00";
    [self.view addSubview:_totalTimeLable];
    
    CGFloat marGin = 3.0;
    CGFloat btnWidth = (sWidth - 18.0)/5;
    CGFloat btnY = sHeight - 50;
    //循环按钮
    _btnCircle = [[UIButton alloc]initWithFrame:CGRectMake(marGin, btnY, btnWidth, 50)];
  //  [_btnCircle setTitle:@"循环" forState:UIControlStateNormal];
  //  _btnCircle.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_btnCircle];
    //上一曲按钮
    _lastMusicBtn = [[UIButton alloc]initWithFrame:CGRectMake(2*marGin+btnWidth, btnY, btnWidth, 50)];
    [_lastMusicBtn addTarget:self action:@selector(lastSong:) forControlEvents:UIControlEventTouchUpInside];
  //  [_lastMusicBtn setTitle:@"上一曲" forState:UIControlStateNormal];
 //   _lastMusicBtn.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_lastMusicBtn];
    //播放按钮
    _btnPlay = [[UIButton alloc]initWithFrame:CGRectMake(3*marGin+btnWidth*2, btnY, btnWidth, 50)];
    //添加响应事件
    [_btnPlay addTarget:self action:@selector(playSong:) forControlEvents:UIControlEventTouchUpInside];
  //  [_btnPlay setTitle:@"播放" forState:UIControlStateNormal];
   // _btnPlay.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_btnPlay];
    
    //下一曲按钮
    _nextMusicBtn = [[UIButton alloc]initWithFrame:CGRectMake(4*marGin+btnWidth*3, btnY, btnWidth, 50)];
   // [_nextMusicBtn setTitle:@"下一曲" forState:UIControlStateNormal];
   // _nextMusicBtn.backgroundColor = [UIColor grayColor];
    [_nextMusicBtn addTarget:self action:@selector(nextSong:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextMusicBtn];
    //菜单按钮
    _menuBtn = [[UIButton alloc]initWithFrame:CGRectMake(5*marGin+btnWidth*4, btnY, btnWidth, 50)];
  //  [_menuBtn setTitle:@"菜单" forState:UIControlStateNormal];
   // _menuBtn.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_menuBtn];
}
-(void)setviewImage{
   self.backview.image=[UIImage imageNamed:@"backgroundImage5"];
    [self.soundSlider setThumbImage:[UIImage imageNamed:@"sliderThumb_small"] forState:UIControlStateNormal];
    [self.progressView setThumbImage:[UIImage imageNamed:@"soundSlider"] forState:UIControlStateNormal];
    [self.btnCircle setBackgroundImage:[UIImage imageNamed:@"circleOpen"]forState:UIControlStateNormal];
    [self.lastMusicBtn setBackgroundImage:[UIImage imageNamed:@"aboveMusic"] forState:UIControlStateNormal];
    [self.nextMusicBtn setBackgroundImage:[UIImage imageNamed:@"nextMusic"] forState:UIControlStateNormal];
    [self.menuBtn setBackgroundImage:[UIImage imageNamed:@"menu"]forState:UIControlStateNormal];
    [self.btnPlay setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
}
-(void)playSong:(id)sender{
    if(!_isplay){
    //播放
        [_player play];
        //修改按钮的图片
        [_btnPlay setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
        _isplay=YES;
    }else{
    //暂停
        [_player pause];
        [_btnPlay setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        _isplay=NO;
    }
}
-(void)lastSong:(id)sender{
    if(_musicNumber==0){
        UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@"错误" message:@"已经是第一首" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"确定", nil];
        [aler show];
    }else{
        _musicNumber=_musicNumber-1;
        NSString *songPath=[[NSBundle mainBundle]pathForResource:[_musicarray[_musicNumber]name] ofType:@"mp3"];
        NSURL *readSong=[NSURL fileURLWithPath:songPath];
        _player=[[AVAudioPlayer alloc]initWithContentsOfURL:readSong error:nil];
        [_player prepareToPlay];
        [self initLrc];
        [_lrcTableView reloadData];
    }
}
-(void)nextSong:(id)sender{
    if(_musicNumber==2){
        UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@"错误" message:@"已经是最后一首" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"确定", nil];
        [aler show];
    }else{
        _musicNumber=_musicNumber+1;
        NSString *songPath=[[NSBundle mainBundle]pathForResource:[_musicarray[_musicNumber]name] ofType:@"mp3"];
        NSURL *readSong=[NSURL fileURLWithPath:songPath];
        _player=[[AVAudioPlayer alloc]initWithContentsOfURL:readSong error:nil];
        [_player prepareToPlay];
        [self initLrc];
        [_lrcTableView reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma make-测试代码
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _timearray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID=@"lrcid";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: cellID];
    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSLog(@"%@",_lrcdictionary[_timearray[indexPath.row]]);
    cell.textLabel.text=_lrcdictionary[_timearray[indexPath.row]];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.backgroundColor=[UIColor clearColor];
    return cell;
}
@end
